<?php
vendor('dbug');
import('@.Model.Account');
/****
* 只负责处理login、logout、和cookie相关
*/
abstract class InitAction extends Action{
    static private $_CONST=array(
        'LOGIN_ERROR_USERNAME'=> -1,
        'LOGIN_ERROR_PASSWORD'=> -2,
        'LOGIN_ERROR_ANSWER'=> -3,
        );
    static public function C($name){
        return self::$_CONST[$name];
    }
    static protected $COOKIE_TIME= 2592000; //保存一个月
    protected $account;
    /****
    * 根据uid获取账户信息
    * @uid 默认cookie值
    */
    public function getInfo(){
        //TODO cookie缓存
        if (! $this->account){
            $u= Cookie::get('account');
            if (!$u){
                return $this->account= null;
            }
            $this->account= $u;
        }
        return $this->account;
    }
    public function getDetail($uid=null){
        //TODO cookie缓存
        if (! $this->account){
            if (!isset($uid)){
                $u= Cookie::get('account');
                $uid=$u['uid'];
            }
            if (!$uid){
                return $this->account= null;
            }
            $am= new AccountModel();
            $this->account= $am->find($uid);
        }
        return $this->account;
    }
    /****
    * @return 用户uid
    */
    public function login($username, $password){
        $am= new AccountModel();
        $u= $am->login($username, $password);
        if ($u[0] <=0){
            //登录失败，参考UCeter
            //
            return $u;
        }
        //TODO: ucenter同步登录
        $this->_afterLogin($u);
        return $u;
    }
    protected function _afterLogin($u){
        //thinkPHP的Cookie将自动对存入值进行加密
        $s= array(
            'uid'=>$u['uid'],
            'username'=>$u['username'],
            'role'=>$u['role'],
            'name'=>$u['username'],     //现在没有"昵称", 于是直接取用户名了...
        );
        Cookie::set('account', $s, self::$COOKIE_TIME);
    }
    public function logout($ck){
        //TODO: check code
        if (1||$ck){
            Cookie::delete('account');
            Cookie::clear();    //.clear()并不会实质上删除cookie
        }
    }
    /****
    * 根据cookie判断账户是否登录
    * @return 0     未登录
    *         >0    uid
    */
    public function isLogged($forceLogin=false){
        $u= $this->getInfo();
        if (!$u){
            if ($forceLogin) {
                $this->redirect('account/login', array(
                    'from'=>__SELF__
                ));
            }
            return false;
        }
        return $u['uid'];
    }
    /*
    public function _Xregister($data){
        if ($vo=$AM->create($data)){
            $vo= $vo+ $data;    //避免覆盖$vo的值
        }
        return false;
    }*/
    protected function _initialize() {
        $this->assign('self', $this->getInfo());
        /*
        //检查是否登录，没有则直接跳转登录页面
        if (! $this->isLogged()){
            if ('logout'== $this->getActionName()){
                return true;
            }
            $this->redirect('login');
            return false;
        }*/
        //import('@.Action.Account#inc','','.php');
        //include('Acount.inc.php');
    }
}
?>